package com.rykj.widget;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.style.ReplacementSpan;


/**
 * Description:
 * <p/>
 * Created by ycw on 2017/7/17.
 */

public class FrameSpan extends ReplacementSpan {

    private final Paint mPaint;
    private int mWidth;
    private int rx;
    private int ry;
    private int paddingLeft = 6;


    public FrameSpan() {
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.RED);
        mPaint.setAntiAlias(true);
        rx = ry = 12;
    }

    @Override
    public int getSize(Paint paint,
            CharSequence text,
            int start,
            int end,
            Paint.FontMetricsInt fm) {
        //return text with relative to the Paint
        mWidth = (int) paint.measureText(text, start, end) + 2 * rx;
        return mWidth;
    }

    @Override
    public void draw(Canvas canvas,
            CharSequence text,
            int start,
            int end,
            float x,
            int top,
            int y,
            int bottom,
            Paint paint) {
        //draw the frame with custom Paint
        x = x + paddingLeft;
        canvas.drawRoundRect(new RectF(x, top, x + mWidth, bottom), rx, ry, mPaint);

        paint.setColor(Color.RED);
        paint.setTextSize(paint.getTextSize() - 2);
        canvas.drawText(text, start, end, x + rx, y, paint);
    }
}
